package cn.wang.heng.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.wang.heng.make.shopCar;
import cn.wang.heng.text.*;
public class buyshop extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public buyshop() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = request.getContextPath();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String account=request.getParameter("account");
		if(account==null || account.equals(""))
			account="";
		if(account.equals("buy"))
			buy(request,response);
		if(account.equals("remove"))
			remove(request,response);
		if(account.equals("clear"))
			clear(request,response);
		
	}

	private void clear(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		ArrayList buylist=(ArrayList)session.getAttribute("buylist");
		buylist.clear();
		response.setHeader("Refresh", "0;URL=shopgoodcar.jsp");
	}

	private void remove(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		ArrayList buylist=(ArrayList)session.getAttribute("buylist");
		String name=request.getParameter("name");
		
		
		
		
		
		
		shopCar mycar=new shopCar();
		mycar.setBuylist(buylist);
		mycar.removegood(name);
		response.setHeader("Refresh", "0;URL=shopgoodcar.jsp");
	}

	private void buy(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		String strid=request.getParameter("id");
		int id=Integer.parseInt(strid);
		ArrayList shangpinlist=(ArrayList)session.getAttribute("shangpinlist");
	//	ShangPin goods=(ShangPin)shangpinlist.get
		ShangPin goods=(ShangPin)shangpinlist.get(id);
		
		ArrayList buylist=(ArrayList)session.getAttribute("buylist");
		if(buylist==null)
			buylist=new ArrayList();
		shopCar mycar=new shopCar();
		mycar.setBuylist(buylist);
		mycar.addgood(goods);
		session.setAttribute("buylist", buylist);
		response.setHeader("Refresh", "0;URL=show.jsp");
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
